﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Odbc;
using System.Configuration;
using System.Data;

namespace Mapping.Cons
{
    class Program
    {
        static MappingEntities map = new MappingEntities();
        static QScheduleEntities qshc = new QScheduleEntities();
        static OdbcConnection MedtrakConnection = new OdbcConnection(ConfigurationManager.ConnectionStrings["MedtrakConnectionString"].ConnectionString);
        
        static void Main(string[] args)
        {
            for (int i = 1; i < 20460; i++)
            {
                var item = qshc.Appointments.Single(p => p.AppointmentID == i);

                if (item.ServiceID == 2)
                {
                    Console.WriteLine(string.Format("{0}\t{1}\t{2}\t{3}", i, item.HN, item.Physician, item.ServiceID));
                    if (item.Physician == "MD023")
                        item.ServiceID = 1;
                    else if (item.Physician == "MD001")
                        item.ServiceID = 1;
                    else if (item.Physician == "MD058")
                        item.ServiceID = 1;
                    else if (item.Physician == "MD002")
                        item.ServiceID = 1;
                    else if (item.Physician == "MD022")
                        item.ServiceID = 1;
                    else if (item.Physician == "MD048")
                        item.ServiceID = 1;
                    else if (item.Physician == "MD050")
                        item.ServiceID = 1;
                    else if (item.Physician == "MD003")
                        item.ServiceID = 3;
                    else if (item.Physician == "MD004")
                        item.ServiceID = 3;
                }
            }

            //qshc.SaveChanges();

            //DateTime search = DateTime.Now;
            //var str = "{" + string.Format("d '{0}-{1}-{2}'", search.Year, search.Month, search.Day) + "}";

            //string MedtrakSQL2 =
            //"SELECT PA_PatMas.PAPMI_No, PA_Adm.PAADM_ADMNo, PA_PatMas.PAPMI_ID,  " +
            //    "PA_PatMas.PAPMI_Name3, PA_PatMas.PAPMI_Name, PA_PatMas.PAPMI_Name2,  " +
            //    "PA_PatMas.PAPMI_Sex_DR, PA_PatMas.PAPMI_DOB, " +
            //    "PA_Adm.PAADM_RowID, PA_Adm.PAADM_Type, PA_Adm.PAADM_EmergencyNo,  " +
            //    "PA_Adm.PAADM_AdmDate, PA_Adm.PAADM_AdmTime,PA_Adm.PAADM_ConvertDate, PA_Adm.PAADM_ConvertTime,   " +
            //    "PA_Adm.PAADM_DischgDate, PA_Adm.PAADM_DischgTime, PA_Adm.Paadm_VisitStatus, " +
            //    "ARC_AuxilInsurType.AUXIT_Code, ARC_AuxilInsurType.AUXIT_Desc,   " +
            //    "ARC_InsuranceType.INST_Code,ARC_InsuranceType.INST_Desc  ,  " +
            //    "PA_Person.PAPER_StName, PA_Person.PAPER_NokPhone, CT_CityArea.CITAREA_Code, CT_CityArea.CITAREA_Desc,   " +
            //    "CT_City.CTCIT_Code, CT_City.CTCIT_Desc, PA_Adm.PAADM_RefDocClinic_DR,   " +
            //    "CT_Province.PROV_Code, CT_Province.PROV_Desc ,CTLOC_Desc,  " +
            //    "CT_CareProv.CTPCP_Code, CT_CareProv.CTPCP_Desc , CT_CareProv.CTPCP_SMCNo  " +
            //"FROM PA_PatMas, PA_Adm, PA_AdmInsurance, ARC_AuxilInsurType , ARC_InsuranceType  , " +
            //    "PA_Person, CT_CityArea, CT_Province, CT_City,  CT_CareProv, CT_Loc " +
            //"WHERE " +
            //    "(PA_Adm.PAADM_PAPMI_DR =* PA_PatMas.PAPMI_RowId) AND   " +
            //    "(PA_Adm.PAADM_RowID =* PA_AdmInsurance.INS_ParRef) AND   " +
            //    "(PA_AdmInsurance.INS_InsType_DR =* SYSTEM.ARC_InsuranceType.INST_RowId) AND  " +
            //    "(PA_PatMas.PAPMI_RowId =* PA_Person.PAPER_PAPMI_DR) AND   " +
            //    "(PA_Person.PAPER_CityArea_DR =* CT_CityArea.CITAREA_RowId) AND   " +
            //    "(PA_Person.PAPER_CT_Province_DR =* CT_Province.PROV_RowId) AND   " +
            //    "(PA_Person.PAPER_CityCode_DR =* CT_City.CTCIT_RowId)  AND  " +
            //    "(PA_Adm.PAADM_AdmDocCodeDR =* CT_CareProv.CTPCP_RowId) AND " +
            //    "(PA_Adm.PAADM_DepCode_DR =* CT_LOC.CTLOC_RowID) AND " +
            //    "(PA_AdmInsurance.INS_AuxInsType_DR =* ARC_AuxilInsurType.AUXIT_RowID) AND " +
            //    "(PA_Adm.PAADM_AdmDate > " + str + ")";

            //MedtrakConnection.Open();
            //OdbcDataAdapter da = new OdbcDataAdapter(MedtrakSQL2, MedtrakConnection);
            //DataSet ds = new DataSet();
            //da.Fill(ds, "Patient");
            //da.Dispose();
            //MedtrakConnection.Close();

            //var pl = from p in ds.Tables["Patient"].AsEnumerable()
            //         select new
            //         {
            //            //Test = p["PAADM_RefDocClinic_DR"].ToString().Trim(),
            //             HN = p["PAPMI_No"].ToString().Replace("-", "").Trim(),
            //             //AN = p["PAADM_ADMNo"].ToString().Replace("-", ""),
            //             PID = p["PAPMI_ID"].ToString(),
            //             Title = p["PAPMI_Name3"].ToString(),
            //             FirstName = p["PAPMI_Name"].ToString(),
            //             LastName = p["PAPMI_Name2"].ToString(),
            //             Sex = p["PAPMI_Sex_DR"] == System.DBNull.Value ? (int?)null : Convert.ToByte(p["PAPMI_Sex_DR"]),
            //             BirthDate = p["PAPMI_DOB"] == System.DBNull.Value ? (DateTime?)null : Convert.ToDateTime(p["PAPMI_DOB"]),
            //             Address = p["PAPER_StName"].ToString(),
            //             //TambonID = p["CITAREA_Code"] == System.DBNull.Value ? (int?)null : Convert.ToInt32(p["CITAREA_Code"]),
            //             TambonName = p["CITAREA_Desc"].ToString().Trim(),
            //             //AmphurID = p["CTCIT_Code"] == System.DBNull.Value ? (int?)null : Convert.ToInt32(p["CTCIT_Code"]),
            //             AmphurName = p["CTCIT_Desc"].ToString().Trim(),
            //             //ProvinceID = p["PROV_Code"] == System.DBNull.Value ? (int?)null : Convert.ToInt32(p["PROV_Code"]),
            //             ProvinceName = p["PROV_Desc"].ToString().Trim(),
            //             //PaymentID = p["AUXIT_Code"].ToString(),
            //             PaymentName = p["AUXIT_Desc"].ToString().Trim(),
            //             AppointmentDate =
            //              p["PAADM_AdmDate"] == System.DBNull.Value ? (DateTime?)null : Convert.ToDateTime(Convert.ToDateTime(p["PAADM_AdmDate"]).ToString("dd/MM/yyyy")),
            //             //AppointmentTime = p["PAADM_AdmTime"] == System.DBNull.Value ? null : p["PAADM_AdmTime"].ToString("00"),
            //             //p["PAADM_AdmTime"] == System.DBNull.Value ? (DateTime?)null : Convert.ToDateTime(Convert.ToDateTime(p["PAADM_AdmTime"]).ToString("dd/MM/yyyy")),
            //             AppointmentTime =
            //                p["PAADM_AdmDate"] == System.DBNull.Value || p["PAADM_AdmTime"] == System.DBNull.Value
            //                ? (DateTime?)null : Convert.ToDateTime(Convert.ToDateTime(p["PAADM_AdmDate"]).ToString("dd/MM/yyyy") + " " + p["PAADM_AdmTime"].ToString()),
            //             //INSTCode = p["INST_Code"].ToString(),
            //             //INSTName = p["INST_Desc"].ToString(),
            //             PhysicianName = p["CTPCP_Desc"].ToString().Trim(),
            //             //PhysicianSMCNO = p["CTPCP_SMCNo"].ToString(),
            //             //MedtrakRowID = (p["PAADM_RowID"]) == System.DBNull.Value ? (int?)null : Convert.ToInt32(p["PAADM_RowID"]),
            //             //ANOLD = p["PAADM_EmergencyNo"].ToString().Replace("-", ""),
            //             //VisitStatus = p["Paadm_VisitStatus"].ToString(),
            //             Phone = p["PAPER_NokPhone"].ToString().Trim(),
            //             LocationName = p["CTLOC_Desc"] == null ? null : p["CTLOC_Desc"].ToString().Trim(),
            //         };

            ////Console.WriteLine(pl.Count());
            //int i = 1;
            //int j = 1;
            //foreach (var item in pl)
            //{
            //    if (item.PhysicianName.Contains("CHECK PACEMAKER"))
            //    {

            //        var a = qshc.Appointments.Where(p => p.Physician == "MD019" && p.HN == item.HN).FirstOrDefault();
            //        if (a != null)
            //        {
            //            a.ServiceID = 9;
            //            Console.WriteLine(string.Format("{0}\t{1}\t{2}", i, item.HN, item.PhysicianName));
            //            i++;

            //        }
            //        j++;
            //        //Console.WriteLine(string.Format("{0}\t{1}\t{2}\t{3}", i, item.HN, item.PhysicianName, item.LocationName));
            //        //i++;
                    
            //    }
            //}

            //Console.WriteLine(j);
            //qshc.SaveChanges();


            //foreach (var que in medtrak.MedtrakQues)
            //{
            //    if(que.AppointmentDate2 == null)
            //        Console.WriteLine(que.HN);
            //}


                //int i = 1;
                //foreach (var que in pl)
                //{
                //    var hn = que.HN.Replace("-", "");
                //    var result = qshc.AppointedPatients.SingleOrDefault(p => p.HN == hn);
                //    if (result == null)
                //    {
                //        Console.WriteLine(string.Format("{0}\t{1}\tNo", i, hn));

                //        var appointedPatient = new AppointedPatient
                //        {
                //            HN = hn,
                //            Title = que.Title,
                //            FirstName = que.FirstName,
                //            LastName = que.LastName,
                //            Sex = que.Sex.Value,
                //            BirthDate = que.BirthDate,
                //            PIN = que.PID != null ? (que.PID.ToString().Length > 13 ? null : que.PID.ToString()) : null,
                //            Payor = que.PaymentName,
                //            Address = que.Address,
                //            Tambon = que.TambonName,
                //            Amphur = que.AmphurName,
                //            Province = que.ProvinceName,
                //            Phone = que.Phone,
                //        };

                //        qshc.AppointedPatients.AddObject(appointedPatient);

                //        var appointment = new Appointment
                //        {
                //            HN = hn,
                //            AppointmentDate = que.AppointmentDate.Value,
                //            AppointmentTime = que.AppointmentTime.Value.ToString("HH:mm"),
                //            ServiceID = map.Map_Locations.Single(p => p.Location == que.LocationName).LocationID,
                //            Physician = map.Map_Physicians.Single(p => p.Physician == que.PhysicianName).PhysicianCode,
                //            VisitDate = null,
                //            //Remark = string.IsNullOrWhiteSpace(Remark) ? null : Remark,
                //        };

                //        var apppointmentDetail = new AppointmentDetail
                //        {
                //            PreOrder = qshc.PreOrders.Single(p => p.PreOrderID == 1)
                //        };
                //        appointment.AppointmentDetails.Add(apppointmentDetail);

                //        var log = new LogAppointment
                //        {
                //            EmployeeCode = "IT007",
                //            LogDateTime = DateTime.Now,
                //            Action = 1, // Add
                //            Appointment = appointment,
                //        };

                //        qshc.Appointments.AddObject(appointment);
                //        qshc.SaveChanges();
                //    }
                //    else  //ถ้ามี AppointmentPatients แล้ว
                //    {
                //        Console.WriteLine(string.Format("{0}\t{1}\tHave", i, hn));
                //        var appointedPatient = new AppointedPatient
                //        {
                //            HN = hn,
                //            Title = que.Title,
                //            FirstName = que.FirstName,
                //            LastName = que.LastName,
                //            Sex = que.Sex == null ? 0 : (int)que.Sex.Value,
                //            BirthDate = que.BirthDate,
                //            PIN = que.PID != null ? (que.PID.ToString().Length > 13 ? null : que.PID.ToString()) : null,
                //            Payor = que.PaymentName,
                //            Address = que.Address,
                //            Tambon = que.TambonName,
                //            Amphur = que.AmphurName,
                //            Province = que.ProvinceName,
                //            Phone = que.Phone,
                //        };

                //        var appointment = new Appointment
                //        {
                //            HN = hn,
                //            AppointmentDate = que.AppointmentDate.Value,
                //            AppointmentTime = que.AppointmentTime.Value.ToString("HH:mm"),
                //            ServiceID = map.Map_Locations.Single(p => p.Location == que.LocationName).LocationID,
                //            Physician = map.Map_Physicians.Single(p => p.Physician == que.PhysicianName).PhysicianCode,
                //            VisitDate = null,
                //            //Remark = string.IsNullOrWhiteSpace(Remark) ? null : Remark,
                //        };

                //        var apppointmentDetail = new AppointmentDetail
                //        {
                //            PreOrder = qshc.PreOrders.Single(p => p.PreOrderID == 1)
                //        };
                //        appointment.AppointmentDetails.Add(apppointmentDetail);

                //        var log = new LogAppointment
                //        {
                //            EmployeeCode = "IT007",
                //            LogDateTime = DateTime.Now,
                //            Action = 1, // Add
                //            Appointment = appointment,
                //        };

                //        qshc.Appointments.AddObject(appointment);
                //        qshc.SaveChanges();
                //    }

                //    i++;
                //}
            }

            //private static int GetMapLocationID(string location)
            //{
            //    return map.Map_Locations.Single(p => p.Location == location).LocationID;
            //}

            //private static string GetMapPhysicianCode(string physician)
            //{
            //    return map.Map_Physicians.Single(p => p.Physician == physician).PhysicianCode;
            //}
    }
}
